package com.open.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.open.bean.OpenSystemRole;
import com.open.bean.RequestRecord;
import com.open.component.JwtComponent;
import com.open.result.OpenResult;
import com.open.service.OpenSystemRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tanyongpeng
 * <p>des</p>
 **/
@RestController
@RequestMapping("/request/open")
@Slf4j
public class OpenSystemRoleController {

    @Autowired
    OpenSystemRoleService openSystemRoleService;

    @Autowired
    JwtComponent jwtComponent;

    @GetMapping("/role/list")
    public OpenResult roleList(HttpServletRequest request){
        String wId = jwtComponent.getUserWId(request);
        List<OpenSystemRole> roleList = openSystemRoleService.listByRole();
        Map<String, List<OpenSystemRole>> listMap = roleList.stream().collect(Collectors.groupingBy(OpenSystemRole::getRoleClass));
        JSONArray array = new JSONArray();

        List<OpenSystemRole> systemRoles = openSystemRoleService.lambdaQuery()
                .eq(OpenSystemRole::getWid, wId).list();
        if (systemRoles.size() != 0){
            setValue("自定义角色",systemRoles,array);
        }

        for (Map.Entry<String, List<OpenSystemRole>> entry : listMap.entrySet()) {
            setValue(entry.getKey(),entry.getValue(),array);
        }

        return OpenResult.success(array);
    }

    @PostMapping("/role/save")
    public OpenResult save(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        String wId = jwtComponent.getUserWId(request);
        if (StrUtil.isBlank(jsonObject.getStr("name"))){
            return OpenResult.error("角色名称不能为空");
        }
        if (StrUtil.isBlank(jsonObject.getStr("desc"))){
            return OpenResult.error("角色描述不能为空");
        }
        OpenSystemRole systemRole = new OpenSystemRole();
        systemRole.setRoleName(jsonObject.getStr("name"));
        systemRole.setRoleValue(jsonObject.getStr("desc"));
        systemRole.setRoleStatus(0);
        systemRole.setCreateTime(new Date());
        systemRole.setWid(Integer.parseInt(wId));
        systemRole.setRoleIcon("");
        systemRole.setRoleType(1);
        boolean save = openSystemRoleService.save(systemRole);
        return OpenResult.save(save);
    }

    public void setValue(String className,List<OpenSystemRole> openSystemRoleList,JSONArray array){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("class",className);
        jsonObject.set("list",openSystemRoleList);
        array.add(jsonObject);
    }

}
